<?php
require_once "MyRabbitMq.php";
use \MyRabbitMq\MyRabbitMq;
use \PhpAmqpLib\Message\AMQPMessage;
use \PhpAmqpLib\Exchange\AMQPExchangeType;

$rabbit = new MyRabbitMq();
$rabbit->setConfirm();
$rabbit->setAckHandler(function (AMQPMessage $message){
    echo $message->body."传递成功!".PHP_EOL;
});
$rabbit->setNAckHandler(function (AMQPMessage $message){
    echo $message->body."传递失败!".PHP_EOL;
});

$rabbit->setExchange('ex1',AMQPExchangeType::DIRECT,false,true);
$rabbit->setQueue('qu1',false,true);
$rabbit->bindQueueExchange('qu1','ex1','key1');
$pro = ['content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT];
for ($i=0;$i<10;$i++){
    $rabbit->sendMsg("msg",$pro,'ex1','key1');
    $rabbit->waitForPendingAckReturns(1); //必须开启
    echo $i.PHP_EOL;
    sleep(1);
}

$rabbit->close();


//
//$rabbit = new MyRabbitMq();
//$ch = $rabbit->getChannel();
//
//
////$rabbit->setConfirm();
////$rabbit->setAckHandler(function (AMQPMessage $message){
////    echo $message."传递成功!".PHP_EOL;
////});
////$rabbit->setNAckHandler(function (AMQPMessage $message){
////    echo $message."传递失败!".PHP_EOL;
////});
//
//$ch->set_ack_handler(function (AMQPMessage $message){
//    echo $message->body."传递成功!".PHP_EOL;
//});
//$ch->set_nack_handler(function (AMQPMessage $message){
//    echo $message->body."传递失败!".PHP_EOL;
//});
//$ch->confirm_select();
//$ch->exchange_declare('ex1',AMQPExchangeType::DIRECT,false,true,false);
//
////$rabbit->setExchange('ex1',\PhpAmqpLib\Exchange\AMQPExchangeType::DIRECT,false,true);
//$ch->queue_declare('qu1',false,true,false,false);
////$rabbit->setQueue('qu1',false,true);
//
//$ch->queue_bind('qu1','ex1','key1');
//
////$rabbit->bindQueueExchange('qu1','ex1','key1');
//
//
//$i =1;
//    $pro = ['content_type' => 'text/plain', 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT];
//    $msg = new AMQPMessage($i,$pro);
//    $ch->basic_publish($msg,'ex1','key1');
//    $ch->wait_for_pending_acks_returns();
//    echo $i.PHP_EOL;
//$ch->close();
//$rabbit->close();